Previous Book Contents Book Index Next

Inside Macintosh: AppleScript Language Guide / Part 2 - AppleScript Language Reference
Chapter 5 - Objects and References / References to Files and Applications


References to Files

You can use either of these forms to refer to any file:

file nameString
alias nameString
where

nameString is a string of the form "Disk:Folder1:Folder2:...:Filename" that specifies exactly where the file is stored or a string that consists of the file's name only. Disk specifies the disk on the local computer on which the application is stored, Folder1:Folder2:... specifies the sequence of folders that you would have to open to find the application on the local computer, and fileName specifies the name of the file. AppleScript doesn't distinguish uppercase letters from lowercase letters in filenames.

If nameString consists of the file's name only, AppleScript attempts to locate the file in the current directory for the application from which the script is being run (for example, Script Editor). The current directory is the folder or volume whose contents you can see when you choose Open or the equivalent command from the application's File menu. By default, the current directory for any application is the folder or volume in which the application is stored; but the current directory may change as you open and close files and folders from within the application. To be sure that a command acts on the correct file, specify the entire pathname, including the names of the volume and the entire sequence of folders that you would have to open to find the file.

If you use a reference of the form file nameString, AppleScript doesn't attempt to locate the file until the script is actually run. When the script is
run, the file must be located in the specified folder (or, if only a filename was provided, in the current directory) for AppleScript to locate it successfully. Some commands, such as the Save command, create a file with the specified name in the specified location if it doesn't already exist.

To save a reference of the form file nameString in a variable, you must use the A Reference To operator as shown in the example that follows. (To use this script successfully, substitute a pathname that corresponds to a volume, folder or folders, and file that actually exist on your computer.)

set fileRef to a reference to file "Hard Disk:June Sales"tell application "Scriptable Text Editor"   open fileRef
end
If you use a reference of the form alias nameString, AppleScript creates an alias for the file--that is, a representation of the file, much like an alias icon on the desktop, that identifies the file no matter where it is located. AppleScript attempts to locate the file whenever you compile the script--that is, whenever you modify the script and then attempt to check its syntax, save it, or run it.

AppleScript treats an alias like a value that can be stored in a variable and passed around within a script. You don't need to use the A Reference To operator. For example, this script first saves an alias in the variable fileRef, then uses the variable in a Tell statement that opens the file.

set fileRef to alias "Hard Disk:June Sales"tell application "Scriptable Text Editor"   open fileRef
end
If you save this script as a script application or compiled script, move the file June Sales to another location, then open the script again, the statement alias "Hard Disk:June Sales" or its equivalent changes to reflect the file's new location, and the script still works correctly.

The difference between the forms file nameString and alias nameString is also apparent when the file in question is located on a remote computer. If you use the form file nameString, AppleScript doesn't attempt to locate the file until you actually run the script. If you use the form alias nameString, AppleScript also attempts to locate the file whenever you compile the script, requiring appropriate access privileges and possibly a password each time.

The actions you can perform on a specific file depend on the way the application that created the file defines a file object. If an application provides its own definition for a file object, AppleScript locates the file as described in this section, but uses the definition in the application's dictionary to determine the characteristics of the object, such as its properties and the commands it can handle. For the Scriptable Text Editor's definition of a file, see page 328.


Previous Book Contents Book Index Next

© Apple Computer, Inc.
13 JUL 1996